﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace WindowsFormsApplication1
{
    public partial class adminpj : Form
    {
        dbutils db = new dbutils();
        public adminpj()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            string staffid = comboBox1.SelectedValue.ToString();
            string staffname = comboBox1.GetItemText(comboBox1.Items[comboBox1.SelectedIndex]);

            string sql = "select * from pingjia where staffid='"+staffid+"'";
            string text = db.select_one(sql, 3);
            if (text == null)
                return;
            string grade = db.select_one(sql, 4);
            textBox1.Text = text;
            textBox2.Text = grade;
        }

        private void adminpj_Load(object sender, EventArgs e)
        {
            string connection;
            string query;
            DataSet myds;
            connection = dbutils.constr;
            query = "select * from staff";
            MySqlConnection conn = new MySqlConnection(connection);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            conn.Open();
            adapter.SelectCommand = new MySqlCommand(query, conn);
            myds = new DataSet();
            adapter.Fill(myds, "Bill");
            //bindingSource3.DataSource = myds.Tables["Bill"].Rows.;
            //comboBox1.DataSource = bindingSource3;
            comboBox1.DataSource = myds.Tables[0];
            comboBox1.DisplayMember = myds.Tables[0].Columns[0].ColumnName;//显示的文本
            comboBox1.ValueMember = myds.Tables[0].Columns[1].ColumnName;//对应的值
            conn.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == null)
                return;
            if (textBox1.Text.Trim() == "")
                return;
            if (textBox2.Text == null)
                return;
            if (textBox2.Text.Trim() == "")
                return;
            string staffid = comboBox1.SelectedValue.ToString();
            string staffname = comboBox1.GetItemText(comboBox1.Items[comboBox1.SelectedIndex]);
            if (MessageBox.Show("确定评价 " + staffname + " 吗？", "提示", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            string sql="update pingjia set text='"+textBox1.Text.Trim()+"',grade="+textBox2.Text+",pj_date='"+DateTime.Now.ToString("yyyy-MM-dd")+"' where staffid='"+staffid+"'";
            //MessageBox.Show(sql);
            if (db.insert(sql) == 0)
            {
                string sql1 = "insert into pingjia(staffid,staffname,pj_date,text,grade) values('" + staffid + "','" + staffname + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + textBox1.Text.Trim() + "'," + textBox2.Text + ")";
                //MessageBox.Show(sql1);
                if (db.insert(sql1) == 0)
                {
                    MessageBox.Show("error!");
                    return;
                }
                MessageBox.Show("操作成功！");
                return;
            }
            MessageBox.Show("操作成功！");
        }

       
    }
}
